/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.x3.purchasing.inquery;

import com.x3.purchasing.account.Account;
import com.x3.purchasing.common.ClassApplicationModule;
import com.x3.purchasing.quotation.Quotation;
import com.x3.purchasing.quotation.QuotationDAO;
import com.x3.purchasing.quotation.QuotationDAOImpl;
import com.x3.purchasing.vendor.Vendor;
import com.x3.purchasing.vendor.VendorDAO;
import com.x3.purchasing.vendor.VendorDAOImpl;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.fileupload.FileUpload;
import org.springframework.dao.DataAccessException;
import org.zkoss.util.media.Media;
import org.zkoss.zhtml.Messagebox;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zul.Div;
import org.zkoss.zul.Label;
import org.zkoss.zul.Row;

/**
 *
 * @author kris
 */
public class InqueryDetailWnd extends ClassApplicationModule {

    private Session session;
    private Label lblJudul;
    private Label lblKeterangan;
    private Label lblTanggalBuka;
    private Label lblTanggalTutup;
    private Label lblUploadStatus;
    private Row fileUpload;
    private Media media;
    private Account account;
    private Vendor vendor;
    private InqueryDAO inqueryDAO;
    private QuotationDAO quotationDAO;
    private VendorDAO vendorDAO;
    private Integer inqueryId;
    private Inquery currentInquery;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

    public void onCreate() throws Exception {
        inqueryDAO = new InqueryDAOImpl();
        quotationDAO = new QuotationDAOImpl();
        vendorDAO = new VendorDAOImpl();
        lblJudul = (Label) getFellow("lblJudul");
        lblKeterangan = (Label) getFellow("lblKeterangan");
        lblTanggalBuka = (Label) getFellow("lblTanggalBuka");
        lblTanggalTutup = (Label) getFellow("lblTanggalTutup");
        lblUploadStatus = (Label) getFellow("lblUploadStatus");
        fileUpload = (Row) getFellow("fileUpload");
        this.loadDataInquery();
        session = Sessions.getCurrent();
        account = (Account) session.getAttribute("ACC_LOGIN");
        try {
            vendor = vendorDAO.getByAccount(account.getId());
            if (currentInquery.getTanggalTutup().before(new Date())) {
                fileUpload.setVisible(false);
            } else {
                fileUpload.setVisible(true);
            }
        } catch (Exception e) {
            fileUpload.setVisible(false);
        }
    }

    private void loadDataInquery() throws Exception {
        session = Sessions.getCurrent();
        inqueryId = (Integer) session.getAttribute("idInquery");
        session.removeAttribute("idInquery");
        currentInquery = inqueryDAO.get(inqueryId);
        lblJudul.setValue(currentInquery.getJudul());
        lblKeterangan.setValue(currentInquery.getKeterangan());
        lblTanggalBuka.setValue(dateFormat.format(currentInquery.getTanggalBuka()));
        lblTanggalTutup.setValue(dateFormat.format(currentInquery.getTanggalTutup()));
    }

    public void upload(Media media) throws Exception {
        if (media.getContentType().endsWith("pdf") || media.getContentType().endsWith("x-zip-compressed")) {
            if (media.getStreamData().available() > 5242880) {
                Messagebox.show("Ukuran image terlalu besar");
            } else {
                this.media = media;
                Inquery inquery = new Inquery();
                inquery.setId(inqueryId);
                Quotation quotation = new Quotation();
                quotation.setTanggal(new Date());
                quotation.setPenawaran(media.getStreamData());
                quotation.setVendor(vendor);
                quotation.setInquery(inquery);
                quotation.setNamaFile(media.getName());
                quotationDAO.insert(quotation);
                lblUploadStatus.setValue("Upload berhasil");
            }
        } else {
            Messagebox.show("Format tidak sesuai");
        }
    }

    public void redirect(String url) throws Exception {
        Div contentDiv = (Div) this.getParent();
        contentDiv.getChildren().clear();
        Executions.createComponents(url, contentDiv, null);
    }
}
